package edu.ynu.se.xiecheng.achitectureclass.controller;

import edu.ynu.se.xiecheng.achitectureclass.common.controller.LogicController;
import edu.ynu.se.xiecheng.achitectureclass.dao.UserDao;
import edu.ynu.se.xiecheng.achitectureclass.entity.User;
import edu.ynu.se.xiecheng.achitectureclass.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/user")
@Api(tags = "用户接口")
public class UserController extends LogicController<UserService, UserDao, User,Long> {
    public UserController(@Autowired UserService userService) {
        super(userService);
    }

    @PostMapping("/register")
    @ApiOperation("用户注册")
    public ResponseEntity<?> register(@RequestBody Map<String, String> params) {
        try {
            // 检查必要参数
            String username = params.get("username");
            String password = params.get("password");
            
            if (username == null || username.trim().isEmpty()) {
                return ResponseEntity.badRequest().body(Map.of(
                    "success", false,
                    "message", "用户名不能为空"
                ));
            }
            
            if (password == null || password.trim().isEmpty()) {
                return ResponseEntity.badRequest().body(Map.of(
                    "success", false,
                    "message", "密码不能为空"
                ));
            }

            User user = new User();
            user.setUsername(username.trim());
            user.setPassword(password);
            
            // 打印日志，便于调试
            System.out.println("Registering user: " + user.getUsername());
            
            Boolean success = super.getService().register(user);
            if (success) {
                return ResponseEntity.ok().body(Map.of(
                    "success", true,
                    "message", "注册成功"
                ));
            } else {
                return ResponseEntity.badRequest().body(Map.of(
                    "success", false,
                    "message", "注册失败"
                ));
            }
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(Map.of(
                "success", false,
                "message", e.getMessage()
            ));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.badRequest().body(Map.of(
                "success", false,
                "message", "注册失败: " + e.getMessage()
            ));
        }
    }

    @PostMapping("/login")
    @ApiOperation("用户登录")
    public ResponseEntity<?> login(@RequestBody Map<String, String> params) {
        try {
            String username = params.get("username");
            String password = params.get("password");
            
            // 打印日志，便于调试
            System.out.println("Login attempt for user: " + username);
            
            boolean loginSuccess = super.getService().login(username, password);
            if (loginSuccess) {
                return ResponseEntity.ok().body(Map.of(
                    "success", true,
                    "message", "登录成功"
                ));
            } else {
                return ResponseEntity.badRequest().body(Map.of(
                    "success", false,
                    "message", "用户名或密码错误"
                ));
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.badRequest().body(Map.of(
                "success", false,
                "message", "登录失败: " + e.getMessage()
            ));
        }
    }
}
